package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyGrandSonComment;
import cn.wolfcode.trip.base.domain.StrategySonComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategySonCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyGrandSonCommentService;
import cn.wolfcode.trip.base.service.IStrategySonCommentService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("strategyComments")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyGrandSonCommentService strategyGrandSonCommentService;
    @Autowired
    private IStrategySonCommentService strategySonCommentService;
    //根据id查询主评论
    @GetMapping("{id}")
    public StrategyComment selectCommentById(@PathVariable Long id){
        return strategyCommentService.selectCommentById(id);
    }

    //根据主评论id查询一级子评论和二级子评论
    @GetMapping("{commentId}/strategySonComments")
    public PageInfo querySonComments( StrategySonCommentQueryObject qo){
        qo.setOrderBy("sc.create_time asc");
        return strategySonCommentService.query(qo);
    }

    @PostMapping("{commentId}/strategySonComment")
    public JsonResult saveOrUpdate(StrategySonComment strategySonComment){
        JsonResult json = new JsonResult();
        try {
            strategySonCommentService.saveOrUpdate(strategySonComment);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    @PostMapping("{commentId}/strategyGrandSonComment")
    public JsonResult saveGrandSonComment(StrategyGrandSonComment strategyGrandSonComment){
        JsonResult json = new JsonResult();
        try {
            strategyGrandSonCommentService.saveOrUpdate(strategyGrandSonComment);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    /**
     * 攻略评论点赞状态查询接口
     * 资源 /strategyComments/thumbup
     * 请求方式 get
     * 请求参数 Long commentId,Long userId
     * 返回结果 JsonResult
     */
    @GetMapping("/thumbupState")
    public JsonResult checkThumbup(Long commentId,Long userId ){
        JsonResult json = new JsonResult();
        try {
            strategyCommentService.checkThumbup(commentId,userId);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    /**
     * 攻略评论点赞接口
     * 资源 /strategyComments/thumbup
     * 请求方式 put
     * 请求参数 Long commentId,Long currentUserId
     * 返回结果 JsonResult
     */
    @PutMapping("/thumbup")
    public JsonResult insertThumbup(Long commentId,Long currentUserId){
        JsonResult json = new JsonResult();
        try {
            strategyCommentService.insertThumbup(commentId,currentUserId);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    /**
     * 取消点赞接口
     * 资源 /strategyComments/thumbup
     * 请求方式 delete
     * 请求参数 Long commentId,Long currentUsserId
     * 返回结果 JsonResult
     */
    @DeleteMapping("/thumbup")
    public JsonResult delete1Thumbup(Long commentId,Long currentUserId){
        JsonResult json = new JsonResult();
        try {
            strategyCommentService.delete1Thumbup(commentId,currentUserId);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
}
